package cc.iits.demo.crane.service.core.biz;

import cc.iits.demo.crane.api.bo.biz.StatusBO;
import cc.iits.demo.crane.api.service.biz.StatusService;
import cc.iits.demo.crane.dao.dataobject.StatusDO;
import cc.iits.demo.crane.dao.mapper.StatusMapper;
import cc.iits.demo.crane.service.core.CraneServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StatusServiceImpl extends CraneServiceImpl<StatusMapper, StatusDO> implements StatusService {

    /**
     * 保存
     *
     * @param status
     * @return
     */
    @Override
    public boolean save(StatusBO status) {
        verify(status);

        StatusDO entity = change(status, StatusDO.class);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return baseMapper.replace(entity) > 0;
    }

    /**
     *
     * @param sid
     * @return
     */
    @Override
    public StatusBO find(String sid) {
        if (StringUtils.isEmpty(sid)) {
            throw new RuntimeException("会话编号不能为空");
        }

        return change(getById(sid), StatusBO.class);
    }

    /**
     * 参数验证
     *
     * @param status
     */
    private void verify(StatusBO status) {
        if (status == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (StringUtils.isEmpty(status.getSid())) {
            throw new RuntimeException("会话编号不能为空");
        }
        if (status.getMachine() == null) {
            throw new RuntimeException("天车不能为空");
        }
        if (status.getDirection() == null) {
            throw new RuntimeException("方向不能为空");
        }
        if (!(status.getDirection() == 1 || status.getDirection() == 2 || status.getDirection() == 3 || status.getDirection() == 4)) {
            throw new RuntimeException("方向错误");
        }
    }
}
